import axios from 'axios'
import {$localStorage} from "@/utils/application-storage.js";

const axiosService = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL, // 可以在 .env 文件中配置
    timeout: 5000 // 请求超时时间
})
const noneTokenUrls = [`/api/auth/oauth/token`, `TenantView.queryMyByCode.query`] // 不需要带token的接口集合
// 请求拦截器
axiosService.interceptors.request.use(
    config => {
        // 在发送请求之前做些什么，例如添加 token
        const token = $localStorage.getToken()
        if (!noneTokenUrls.includes(config.url) && token) config.headers.Authorization = `Bearer ${token}`

        config.url = `/${$localStorage.getTenantCode()}${config.url}`
        return config
    },
    error => {
        // 处理请求错误
        console.log(error)
        return Promise.reject(error)
    }
)

// 响应拦截器
axiosService.interceptors.response.use(
    response => {
        if(response.data.exception){
            return Promise.reject({response})
        }

        // 对响应数据做点什么
        return response
    },
    error => {
        // 处理响应错误
        console.log('err' + error)
        return Promise.reject(error)
    }
)

export const getAction = function (url = '', param = {}) {
    return axiosService.get(url, param)
}
export const postAction = function (url = '', data = {}, config = {}) {
    // if (!config['headers']) {
    //     config['headers'] = { 'Content-Type': 'application/json' }
    // }
    //
    // // 如果请求地址是FormDynamicTransactor或者FormDynamicView,则将表编码作为queryParams带上
    // if (url.includes('FormDynamicTransactor') && data.data.tableCode && !url.includes('tableCode=')) {
    //     url += '?tableCode=' + data.data.tableCode
    // }
    // if (url.includes('FormDynamicView') && data.params.tableCode && !url.includes('tableCode=')) {
    //     url += '?tableCode=' + data.params.tableCode
    // }
    // // 进行加解密处理
    // // 如果开启加密，则按加密提交，当程序没有握手，则阻止本次请求，改成握手请求
    // const enc = local.get('E-o-enc')
    //
    // if (enc && enc !== 'N' && (url.endsWith('command') || url.endsWith('query') || url.includes('custom-api'))) {
    //     var key = null
    //     if (enc === 'F') {
    //         key = fixSmsKey
    //     } else {
    //         var timestamp = new Date().getTime()
    //         var baseNum = parseInt(timestamp / 100000)
    //         key = baseNum + '00000000'
    //     }
    //
    //     key = stringToHex(key)
    //     var sm4 = Sm4Crypto
    //     var encryptData = sm4.encrypt(JSON.stringify(data), key)
    //     return axiosService.axios.post(url, 'enc' + encryptData, config)
    // } else {
    //     return axiosService.post(url, data, config)
    // }
    // // if (!handdshake) {
    // //   axiosService.axios.post(`/api/epms/handshake`, {} + encryptData, config)
    // // } else {
    // // }

    return axiosService.post(url, data, config)
}
export const putAction = function (url = '', data = {}, config = {}) {
    return axiosService.put(url, data, config)
}
export const deleteAction = function (url = '', config = {}) {
    return axiosService.delete(url, config)
}
export const downloadAction = function (params = '') {
    const tenantCode = JSON.parse(localStorage.getItem('E-o-tenant-code')).content
    window.open(`${baseURL}/${tenantCode}/api/epms/file/proxyDown?` + params)
}
